package com.panshi.controller;

import com.panshi.service.ICommentService;
import com.panshi.vo.qa.Comment;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Pages;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
@Api(tags = "评论业务接口类")
@RequestMapping(value = "/comment", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CommentController {
    @Autowired
    ICommentService commentService;

    //评论添加
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("评论添加")
    @ApiImplicitParam(name="comment",value = "评论属性类")
    DataResultQA addComment(@RequestBody Comment comment){
        log.info("请求路径：{},参数：{}","/comment/add",comment);
        DataResultQA<Integer> dataResultQA = new DataResultQA<>();
        boolean b = (comment.getCContent() == null || comment.getCType() == 0) || (comment.getCParentId() == 0 && comment.getCTextId() == 0);
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Integer id = commentService.addComment(comment);
        if(id > 0){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("添加完成！");
            dataResultQA.setData(id);
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("添加失败！");
        }
        return dataResultQA;
    }

    //分页查询
    @PostMapping("/inquirePages")
    @ResponseBody
    @ApiOperation("分页查询评论")
    @ApiImplicitParam(name = "pages",value = "查询条件")
    DataResultQA<Pages<Comment>> inquirePagesComment(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/comment/inquirePages",pages);
        DataResultQA<Pages<Comment>> dataResultQA = new DataResultQA();
        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Pages<Comment> p= commentService.inquirePagesComment(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("分页查询评论完成！");
        dataResultQA.setData(p);
        return dataResultQA;
    }

    //分页查询回复
    @PostMapping("/inquirePagesRevert")
    @ResponseBody
    @ApiOperation("分页查询回复")
    @ApiImplicitParam(name = "pages",value = "查询条件")
    DataResultQA<Pages<Comment>> inquirePagesRevert(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/comment/inquirePagesRevert",pages);
        DataResultQA<Pages<Comment>> dataResultQA = new DataResultQA();
        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Pages<Comment> p= commentService.inquirePagesRevert(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("分页查询回复完成！");
        dataResultQA.setData(p);
        return dataResultQA;
    }

    //查询总记录数
    @PostMapping("/inquireSum")
    @ResponseBody
    @ApiOperation("查询总记录数")
    @ApiImplicitParam(name = "pages",value = "查询条件")
    DataResultQA<Integer> inquireCommentSum(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/comment/inquireSum",pages);
        DataResultQA<Integer> dataResultQA = new DataResultQA();

        if(pages.getStartPage() < 0 || pages.getPageSize() <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }

        Integer sum= commentService.inquireCommentSum(pages);

        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("查询数据条数！");
        dataResultQA.setData(sum);
        return dataResultQA;
    }

    //通过id查询评论
    @PostMapping("/inquireCommentById/{id}")
    @ResponseBody
    @ApiOperation("通过id查询评论")
    @ApiImplicitParam(name = "id",value = "评论id")
    DataResultQA<Comment> inquireCommentById(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/comment/inquireCommentById/{id}",id);
        DataResultQA<Comment> dataResultQA = new DataResultQA<>();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Comment comment= commentService.inquireCommentById(id);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("根据id查询！");
        dataResultQA.setData(comment);
        return dataResultQA;
    }

    //通过id查询回复
    @PostMapping("/inquireRevertById/{id}")
    @ResponseBody
    @ApiOperation("通过id查询回复")
    @ApiImplicitParam(name = "id",value = "评论id")
    DataResultQA<Comment> inquireRevertById(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/comment/inquireRevertById/{id}",id);
        DataResultQA<Comment> dataResultQA = new DataResultQA<>();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Comment comment= commentService.inquireRevertById(id);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("根据id查询！");
        dataResultQA.setData(comment);
        return dataResultQA;
    }

    //删除(标记删除)
    @PostMapping("/delete/{id}")
    @ResponseBody
    @ApiOperation("删除(标记删除)")
    @ApiImplicitParam(name = "id",value = "评论id")
    DataResultQA deleteComment(@PathVariable int id){
        log.info("请求路径：{},参数：{}","/comment/delete/{id}",id);
        DataResultQA dataResultQA = new DataResultQA();
        if(id <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = commentService.deleteComment(id);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("删除(标记删除)完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("删除(标记删除)失败！");
        }
        return dataResultQA;
    }

    //评论点赞
    @PostMapping("/giveALike/{id}")
    @ResponseBody
    DataResultQA giveALike(@PathVariable int id){
        DataResultQA dataResultQA = commentService.giveALike(id);
        return dataResultQA;
    }

    //评论赞减一
    @PostMapping("/cancelGiveALike/{id}")
    @ResponseBody
    DataResultQA cancelGiveALike(@PathVariable int id){
        DataResultQA dataResultQA = commentService.cancelGiveALike(id);
        return dataResultQA;
    }

    //用于用户查询自己发布的评论
    @PostMapping("/inquireMy")
    @ResponseBody
    @ApiOperation("用于用户查询自己发布的评论")
    @ApiImplicitParam(name = "pages",value = "查询条件")
    DataResultQA<Pages<Comment>> inquireMyComment(@RequestBody Pages pages){
        log.info("请求路径：{},参数：{}","/comment/inquireMy",pages);
        DataResultQA<Pages<Comment>> dataResultQA = new DataResultQA();
        boolean b = pages.getStartPage() < 0 || pages.getPageSize() <= 0 || pages.getUserId() <= 0;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Pages<Comment> p= commentService.inquireMyComment(pages);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("My分页查询完成！");
        dataResultQA.setData(p);
        return dataResultQA;
    }

}
